#!/bin/bash
#author: <fanyuantao>
#date: 2024-08-08
#desc: This script is used to scan large files in a folder

scan_folder_path=$(pwd)

function scan_folder() {
    
    read -p "Please input the folder path(default is current folder):" folder_path
    if [ ! -z "$folder_path" ]; then
        scan_folder_path=$folder_path
    fi

    if [ ! -d "$scan_folder_path" ]; then
        echo "$scan_folder_path does not exist!"
        exit 1
    fi

    echo "Scanning folder: $scan_folder_path"

    du $scan_folder_path -Sh | sort -rh | head -n 10
}

function scan_large_files() {
    read -p "Please input the folder path(default is current folder):" folder_path
    if [ ! -z "$folder_path" ]; then
        scan_folder_path=$folder_path
    fi

    if [ ! -d "$scan_folder_path" ]; then
        echo "$scan_folder_path does not exist!"
        exit 1
    fi

    echo "Scanning folder: $scan_folder_path"

    find $scan_folder_path -type f -exec du -Sh {} + | sort -rh | head -n 10
}

function select_mode() {
    while true; do
        echo ""
        echo "  1. Scan folder"
        echo "  2. Scan Large Files"
        echo "  0. Exit"
        read -p "Please select the mode:" selectd
        case $selectd in
            1) scan_folder;;
            2) scan_large_files;;
            0) exit 0;;
            *) echo "Invalid input!";;
        esac
    done
}

function main() {
    select_mode
}

main